package cn.cvs.controller;

import cn.cvs.pojo.SysUser;
import cn.cvs.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

@Controller
public class LoginController_html {

    @Resource
    UserService userService;

    @ResponseBody
    @RequestMapping("/toindex")
    public String toindex(){
        return "index";
    }

    @RequestMapping("/welcome") //欢迎页
    public String welcome(HttpServletRequest request, Model model){

        //添加内置对象的值用于测试
        request.setAttribute("test_request","这是放在request域中的值");
        request.setAttribute("test_session","这是放在session域中的值");
        model.addAttribute("date",new Date());

        return "welcome";
    }

    @RequestMapping(value = {"/login.html","/login.htm"})
    public String toLogin(Map<String,Object> map){
        map.put("name","hello thymeleaf");
        return "login";
    }
    @RequestMapping(value = {"/doLogin.html"})
    public String doLogin(@RequestParam("username") String username
                        , @RequestParam("password") String password
                        , HttpServletRequest request
                          ){
        SysUser loginUser = userService.doLogin(username, password);
        if (null != loginUser){
            request.getSession().setAttribute("currUser",loginUser);
            return "index";
        }else{
            return "login";
        }
    }
}
